﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA.Mvc.Data.Entity
{
    public class PrivateMessageRepository : EntityRepository<PrivateMessage>, IPrivateMessageRepository
    {
        public PrivateMessageRepository() : base() { }

        public PrivateMessageRepository(DB dbContext) : base(dbContext) { }

        public IQueryable<PrivateMessage> GetReceives(string userName)
        {
            return dbSet.Where(p => p.Receivers.Contains(userName)).OrderByDescending(p => p.Sent);
        }

        public IQueryable<PrivateMessage> GetReceives(string userName, out int total, int pageIndex = 0, int pageSize = 50)
        {
            var skips = pageIndex * pageSize;
            total = dbSet.Count(p => p.Receivers.Contains(userName));
            var query =dbSet.Where(p => p.Receivers.Contains(userName)).OrderByDescending(p => p.Sent);
            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }

        public IQueryable<PrivateMessage> GetSent(string userName)
        {
            return dbSet.Where(p => p.Sender.Equals(userName)).OrderByDescending(p => p.Sent);
        }

        public IQueryable<PrivateMessage> GetSent(string userName, out int total, int pageIndex = 0, int pageSize = 50)
        {
            var skips = pageIndex * pageSize;
            total = dbSet.Count(p => p.Sender.Equals(userName));
            var query = dbSet.Where(p => p.Sender.Equals(userName)).OrderByDescending(p => p.Sent);
            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }
    }
}
